package com.carkeeper.user.service.impl;

import com.carkeeper.user.dao.MenuDao;
import com.carkeeper.user.pojo.Menu;
import com.carkeeper.user.service.MenuService;
import com.github.pagehelper.Page;
import com.github.pagehelper.PageHelper;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;
import tk.mybatis.mapper.entity.Example;

import java.util.LinkedHashSet;
import java.util.List;
import java.util.Map;

/**
 * @ClassName MenuServiceImpl
 * @Description TODO
 * @Author yangbin
 * @Date 2020/11/23 14:28
 * @Version 1.0
 */

@Service
@Transactional
public class MenuServiceImpl implements MenuService {

    @Autowired
    private MenuDao menuDao;

    /**
     * 新增菜单
     * @param menu
     * @return
     */
    @Override
    public int addMenu(Menu menu) {
        return menuDao.insertSelective(menu);
    }

    /**
     * 删除菜单
     * @param id
     * @return
     */
    @Override
    public int deleteById(Integer id) {
        return menuDao.deleteByPrimaryKey(id);
    }

    /**
     * 修改菜单
     * @param menu
     * @return
     */
    @Override
    public int updateMenu(Menu menu) {
        return menuDao.updateByPrimaryKeySelective(menu);
    }

    /**
     * 根据id查询
     * @param id
     * @return
     */
    @Override
    public Menu findById(Integer id) {
        return menuDao.selectByPrimaryKey(id);
    }

    /**
     * 查询子菜单
     * @param menuId
     * @return
     */
    @Override
    public List<Menu> findChildByParentRoleId(Integer menuId) {
        return menuDao.findChildByParentRoleId(menuId);
    }

    /**
     * 查询所有
     * @return
     */
    @Override
    public List<Menu> findAll() {
        return menuDao.selectAll();
    }


    /**
     * 分页条件查询
     * @param searchMap
     * @param page
     * @param size
     * @return
     */
    @Override
    public Page<Menu> queryPageByExample(Map<String, Object> searchMap, int page, int size) {
        PageHelper.startPage(page, size);
        Example example = createExample(searchMap);
        return (Page<Menu>) menuDao.selectByExample(example);
    }

    @Override
    public LinkedHashSet<Menu> findMenuListByRoleId(Integer roleId) {
        return menuDao.findMenuListByRoleId(roleId);
    }

    @Override
    public int addMenuListByRoleIdAndMenuId(Integer roleId, Integer menuId) {
        return menuDao.addMenuListByRoleIdAndMenuId(roleId, menuId);
    }

    @Override
    public int deleteMenuListByRoleId(Integer roleId) {
        return menuDao.deleteMenuListByRoleId(roleId);
    }

    /**
     * 查询条件
     * @param searchMap
     * @return
     */
    private Example createExample(Map<String, Object> searchMap) {
        Example example = new Example(Menu.class);
        Example.Criteria criteria = example.createCriteria();
        // 订单id
        if (searchMap.get("name") != null && !"".equals(searchMap.get("name"))) {
            criteria.andEqualTo("name", searchMap.get("name"));
        }
        // 订单id
        if (searchMap.get("level") != null && !"".equals(searchMap.get("level"))) {
            criteria.andEqualTo("level", searchMap.get("level"));
        }
        return example;
    }
}
